/*
 * kdogs - Kick dogs, a lightweight watchdog kicker
 *
 * Copyright (C) 2007 Instituto Nokia de Tecnologia
 *
 * Author: Osvaldo Santana Neto <osvaldo.neto@openbossa.org>
 *         Abner Silva <abner.silva@openbossa.org>
 *
 *	This program is free software; you can redistribute it and/or modify it
 *	under the terms of the GNU General Public License as published by the
 *	Free Software Foundation version 2 of the License.
 *
 *	This program is distributed in the hope that it will be useful, but
 *	WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *	General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License along
 *	with this program; if not, write to the Free Software Foundation, Inc.,
 *	51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

#include <errno.h>
#include <stdio.h>
#include <stddef.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/resource.h>
#include <kdogs_cmd.h>

int main(int argc, char **argv) {
    struct sockaddr_un saddr;
    socklen_t addrlen;

    int count = 0;
    int kdogsd_sock = 0;

    /* define command */
    kdogs_cmd command = {_KDOGS_CMD_KICK, 0};

    /* create socket */
    kdogsd_sock = socket (AF_LOCAL, SOCK_DGRAM, 0);
    if (kdogsd_sock < 0) {
        fprintf (stderr, "error getting socket: %s\n", strerror (errno));
        return -1;
    }

    /* set target */
    memset (&saddr, 0x00, sizeof (saddr));
    saddr.sun_family = AF_LOCAL;
    strcpy (&saddr.sun_path[1], "/kdogs");
    addrlen = offsetof (struct sockaddr_un, sun_path) + strlen (saddr.sun_path + 1) + 1;

    /* send command */
    count = sendto (kdogsd_sock, &command, sizeof (command), 0, (struct sockaddr *)&saddr, addrlen);

    if (count > 0)
        fprintf (stderr, "command %#x sent!\n", command.cmd);
    else
        fprintf (stderr, "error sending command! %s\n", strerror(errno));

    /* close socket */
    close (kdogsd_sock);

    return 0;
}
